<template>
  <div>
    <el-switch v-model="$data.data.enable"
    :disabled="!$hasPermission('sys:bots:switch')"
               @change="changeSwitch($event,$data.data,'enable')">
    </el-switch>
  </div>
</template>

<script>
import { LinkComponent } from './LinkComponent.js'

export default {
  data () {
    return {
      data: {}
    }
  },
  methods: {
    changeSwitch (e, item, type) {
      var _self = this
      this.loading = true
      const itemMessage = item.paramsValue
      let enable = item.enable
      let send = item.send
      let sendEscalation = item.sendEscalation
      if (type === 'enable') {
        send = false
        sendEscalation = false
      }
      const params = {
        id: item.id,
        enable: enable,
        paramsValue: {
          ...itemMessage,
          send: send,
          sendEscalation: sendEscalation
        }
      }
      this.$interface.bots.updateTenantScriptSwitchApi(params)
        .then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            this.dataList = []
            this.total = 0
            return this.$message.error(res.msg)
          }
          this.$message.success(res.msg)
          LinkComponent.$emit('initPageList')
        }).catch(() => {
          _self.$data.data.enable = !_self.$data.data.enable
          this.loading = false
        })
    }
  }
}
</script>

<style>
</style>
